package com.base.cn.platform.os.controller.mobile.op.affiche;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.service.manage.op.affiche.OpReceiveAfficheService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * H5用户消息controller
 *
 * @author zsw
 * @create 2018-06-13
 */
@Controller
@RequestMapping("/mobile/uc/op/affiche")
public class MobileAfficheController extends MobileBaseController {

    @Autowired
    private OpReceiveAfficheService receiveAfficheService;
    @Autowired
    private CusUserService cusUserService;

    /**
     * 获取用户私信
     *
     * @param request
     * @return
     */
    @RequestMapping("/queryUserLetterList")
    public String queryUserLetterList(HttpServletRequest request) {
        return resultPage("/op/affiche/u-letter");
    }
    /**
     * 获取用户私信详情
     *
     * @param request
     * @return
     */
    @RequestMapping("/queryUserLetterInfo")
    public String queryUserLetterInfo(HttpServletRequest request) {
        return resultPage("/op/affiche/u-letter-info");
    }

    /**
     * 获取用户消息
     *
     * @param request
     * @return
     */
    @RequestMapping("/queryUserAfficheList")
    public String queryUserAfficheList(HttpServletRequest request) {
        //TODO 清空缓存中的未读消息数
        //redisCache.set(ReceiveAfficheBiz.USER_MSG_NUM_TAG + userId, "", CacheConstants.ONE_DAY);
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        receiveAfficheService.saveReceiveAffichBatch(userId);
        return resultPage("/op/affiche/u-affiche");
    }

    /**
     * ajax加载查询消息列表
     *
     * @param request
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajaxQueryUserAfficheList")
    public String ajaxQueryUserAfficheList(Model model,
                                           HttpServletRequest request,
                                           @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage) {
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> cusUser = cusUserService.userInfo(loginUserId, false, false, false, false, false, false, false);
        cusUser.replace("lastSystemTime", new Date());
        cusUserService.updateUser(cusUser);
        HashMap<String, Object> receiveAffiche = new HashMap<>();
        receiveAffiche.put("userId", loginUserId);
        receiveAffiche.put("order",2);
        Map<String, Object> afficheMap = receiveAfficheService.queryAjaxReceiveAfficheList(currentPage, receiveAffiche, true, true, true);
        //遍历接收来自班级的消息,替换班级路径
        if (StringUtils.isNotEmpty(afficheMap)){
            List<Map<String, Object>> list = (List<Map<String, Object>>) afficheMap.get("list");
            list.forEach(e->{
                String context = (String) e.get("context");
                if (context.contains("web/classes/info")){
                    String replaceContext = context.replace("web/classes/info?id", "mobile/class/info?classId");
                    e.put("context",replaceContext);
                }
            });
        }
        model.addAttribute("dataMap", afficheMap);
        return resultPage("/op/affiche/u-ajax-affiche");
    }

    /**
     * 修改消息状态
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateReceiveStatus")
    @ResponseBody
    public Map<String,Object> updateReceiveStatus(HttpServletRequest request) {

        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return receiveAfficheService.updateReceiveArticleStatusById(userId);

    }
    /**
     * 获取消息数量
     *
     * @param request
     * @return
     */
    @RequestMapping("/countReceiveAffiche")
    @ResponseBody
    public Map<String,Object> countReceiveAffiche(HttpServletRequest request) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return receiveAfficheService.countReceiveAffiche(userId);
    }

    /**
     * 删除消息
     * @param id 消息ID
     * @return Map<String,Object>
     */
    @RequestMapping("/deleteReceive")
    @ResponseBody
    public Map<String,Object> deleteReceive(HttpServletRequest request,@RequestParam("id") String id){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return receiveAfficheService.deleteReceiveAfficheIds(id,userId);
    }

    /**
     * 消息公告修改状态
     * @param id 消息ID
     * @return Map<String,Object>
     */
    @RequestMapping("/updateReceiveIdStatus")
    @ResponseBody
    public Map<String,Object> updateReceiveIdStatus(HttpServletRequest request,@RequestParam("id") BigDecimal id){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return receiveAfficheService.updateReceiveAfficheById(id,userId);
    }

}
